package com.itce.arco.test;

import java.util.Arrays;

public class tset11 {
    public static void main(String[] args) {
        /*给定一个整数数组 nums，将数组中的元素向右轮转 k 个位置，其中 k 是非负数。*/
/*        输入: nums = [1,2,3,4,5,6,7], k = 3
        输出: [5,6,7,1,2,3,4]
        解释:
        向右轮转 1 步: [7,1,2,3,4,5,6]
        向右轮转 2 步: [6,7,1,2,3,4,5]
        向右轮转 3 步: [5,6,7,1,2,3,4]*/
        int[] ints = {1, 2, 3, 4, 5, 6, 7};
        rotate(ints,3);
        System.out.println(Arrays.toString(ints));
    }
    public static void rotate(int[] nums, int k) {
        int[] ints = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            ints[(i+k)%nums.length] = nums[i];
        }
        System.arraycopy(ints,0,nums,0,nums.length);
    }
    public static void rotate1(int[] nums, int k) {
        int n = nums.length;
        k = k % n;
        int count = gcd(k, n);
        for (int start = 0; start < count; ++start) {
            int current = start;
            int prev = nums[start];
            do {
                int next = (current + k) % n;
                int temp = nums[next];
                nums[next] = prev;
                prev = temp;
                current = next;
            } while (start != current);
        }
    }

    public static int gcd(int x, int y) {
        return y > 0 ? gcd(y, x % y) : x;
    }

}
